package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.BallLight;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 球泡灯数据库操作
 */

public class BallLightDao {
    private Context mContext;
    private Dao<BallLight, Integer> ballLightDaoOpe;
    private DatabaseHelper helper;

    public BallLightDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            ballLightDaoOpe = helper.getDao(BallLight.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     *
     */
    public void add(BallLight ballLight) {
        try {
            ballLight.setChecked(false);
            ballLightDaoOpe.create(ballLight);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int updateBallLight(BallLight ballLight) {
        try {
            ballLight.setChecked(false);
            return ballLightDaoOpe.update(ballLight);
            //    LogHelper.i("updateLightBallLightnumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public BallLight get(int id) {
        try {
            return ballLightDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public BallLight getByUuid(String uuid) {
        List<BallLight> ballLightList = null;
        try {
            ballLightList = ballLightDaoOpe.queryBuilder().where().eq
                    ("uuid", uuid).query();
            if (ballLightList != null && ballLightList.size() > 0) {
                return ballLightList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public List<BallLight> getAllBallLight() {
        List<BallLight> ballLightList = new ArrayList<>();
        try {
            ballLightList = ballLightDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ballLightList;
    }

    public BallLight getById(String groupId,int category) {
        List<BallLight> ballLightList = null;
        try {
            ballLightList = ballLightDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (ballLightList != null && ballLightList.size() > 0) {
                return ballLightList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<BallLight> getFamilyBallLights(String familyId,int category) {
        List<BallLight> ballLightList = new ArrayList<>();
        try {
            ballLightList = ballLightDaoOpe.queryBuilder().orderBy("groupId",true).where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ballLightList;
    }



    public BallLight getFamilyBallLight(String familyId,String groupId) {

        try {
            return ballLightDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("groupId", groupId).queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int delete(BallLight ballLight) {
        try {
            return ballLightDaoOpe.delete(ballLight);
            //    LogHelper.i("deleLightBallLightnumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = ballLightDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<BallLight> ballLightList) {
        try {
            int number = ballLightDaoOpe.delete(ballLightList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyBallLights(String familyId) {
        try {
            List<BallLight> ballLightList = getFamilyBallLights(familyId,1);
            int number = ballLightDaoOpe.delete(ballLightList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public BallLight getBallLightByName(String familyId, String name,int category) {
        try {
            List<BallLight> ballLightList = ballLightDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (ballLightList != null && ballLightList.size() > 0)
                return ballLightList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
